package com.hns.tool.pub;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.List;

import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.util.ReflectUtil;

/**
 * 文本工具类
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
public class TxtUtil {
    
	/**
	 * 将list中对象属性写入指定文件
	 * @param filePath 文件路径
	 * @param list 传入的list, 对象必须包含paramName和paramValue属性
	 * @throws IOException
	 * @author:Fisher
     * @email:zhuangcaijin@126.com
	 */
    @SuppressWarnings("resource")
    public static void writeProperties(String filePath, List<?> list) throws IOException {
        RandomAccessFile mm = null;
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(filePath);
            StringBuilder content = new StringBuilder();
            
            for(Object o : list){
                String fieldName = ReflectUtil.getFieldValue(o, "paramName").toString();
                String fieldValue = ReflectUtil.getFieldValue(o, "paramValue").toString();
                if(PubUtil.isEmpty(fieldName)){
                    throw new BusinessException(MsgType.ERROR.getFlag(), "参数对象必须含有paramName和paramValue属性");
                }
                content = content.append(fieldName+ "=" +fieldValue+"\n");
            }
            
            fos.write(content.toString().getBytes("GB2312"));
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mm != null) {
                mm.close();
            }
        }
    }
    
    /**
     * 从list的对象中获取属性名属性值,转化为指定格式文本字符串
     * @param list 目标list
     * @return 生成的字符串
     * @throws IOException
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    public static String getTxtStr(List<?> list) throws IOException {
        StringBuilder content = new StringBuilder();
        try {
            
            for(Object o : list){
                String fieldName = ReflectUtil.getFieldValue(o, "paramName").toString();
                String fieldValue = ReflectUtil.getFieldValue(o, "paramValue").toString();
                if(PubUtil.isEmpty(fieldName)){
                    throw new BusinessException(MsgType.ERROR.getFlag(), "参数对象必须含有paramName和paramValue属性");
                }
                content = content.append(fieldName+ "=" +fieldValue+"\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content.toString();
    }
    
    public static void main(String args[]) throws IOException {
/*        List<TxtVo> list = new ArrayList<TxtVo>();
        TxtVo vo1 = new TxtVo("EquipID","05920007");
        TxtVo vo2 = new TxtVo("PhoneCall","18250715540");
        TxtVo vo3 = new TxtVo("Ip","192.168.100.178");
        TxtVo vo4 = new TxtVo("Port","80");
        TxtVo vo5 = new TxtVo("ServerIpTcp1","110.80.24.92");
        TxtVo vo6 = new TxtVo("ServerPortTcp1","8889");
        TxtVo vo7 = new TxtVo("ServerIpTcp2","192.168.100.190");
        TxtVo vo8 = new TxtVo("ServerPortTcp2","8891");
        TxtVo vo9 = new TxtVo("ServerIpUdp1","192.168.100.190");
        TxtVo vo10 = new TxtVo("ServerPortUdp1","8889");
        TxtVo vo11 = new TxtVo("ServerIpUdp2","192.168.100.190");
        TxtVo vo12 = new TxtVo("ServerPortUdp2","8889");
        TxtVo vo13 = new TxtVo("FileIpTcp","59.57.240.218");
        TxtVo vo14 = new TxtVo("FilePortTcp","21");
        TxtVo vo15 = new TxtVo("ChePaiHao","闽CDXC5537");
        TxtVo vo16 = new TxtVo("AccidentFilePath","/hnsfiles/upload/communimodel/accident");
        TxtVo vo17 = new TxtVo("DriveRecordFilePath","/mnt/lxy/stor");
        list.add(vo1);
        list.add(vo2);
        list.add(vo3);
        list.add(vo4);
        list.add(vo5);
        list.add(vo6);
        list.add(vo7);
        list.add(vo8);
        list.add(vo9);
        list.add(vo10);
        list.add(vo11);
        list.add(vo12);
        list.add(vo13);
        list.add(vo14);
        list.add(vo15);
        list.add(vo16);
        list.add(vo17);
        writeProperties("f:\\commu.conf", list);*/
    }
}
